// pages/component/checkInfo/checkInfo.js
let myBehavior = require("../../../utils/dataInit")
const app = getApp()
Component({
  options: {
    addGlobalClass: true
  },
  behaviors: [myBehavior],
  properties: {
    detailedMsg: Object,
    basetpId: Number,
    changetpId: Number
  },

  data: {
    t_changetpId: '',
    t_basetpId: '',
    _detailedMsg: {},
    isShowLearnText: false,
    isShowWantText: false,
    requestSecondary: '',
  },
  //  observers:{ 
  //  basetpId:function(){this.setData({ t_basetpId:this.properties.basetpId })    }
  //     },
  attached() {
    this.dataInit()

  },
  methods: {

    detailedMsgShow() {

      this.setData({
        '_detailedMsg': this.properties.detailedMsg,
        't_changetpId': this.properties.changetpId,
        't_basetpId': this.properties.basetpId
      })          //前面的key加引号和不加引号有什么区别?
    },
    // 下面的方法是控制border的转换 使用户能够看到完整的信息 但是没有修改功能
    showLearnText() {
      if (!this.data.isShowWantText) {
        this.setData({
          'isShowLearnText': !this.data.isShowLearnText
        })
      }
    },
    backText() {
      this.setData({
        'isShowLearnText': !this.data.isShowLearnText
      })
    },
    showWantText() {
      if (!this.data.isShowLearnText) {
        this.setData({
          'isShowWantText': !this.data.isShowWantText
        })
      }
    },
    backWantText() {
      this.setData({
        'isShowWantText': !this.data.isShowWantText
      })
    },
    //测试插入 


    //提交信息
    sendRequest() {
      let that = this

      wx.showToast({
        title: '提交中',
        icon: 'loading',
        duration: 1000
      })
      let subDepartment = this.data.inUserInfo[1].secondary

      if (subDepartment.length == 0) {
        this.setData({
          'requestSecondary': JSON.stringify(''),
        })
      } else {
        this.setData({
          'requestSecondary': subDepartment.join(",")
        })
      }


      console.log('tpid' + this.data.t_changetpId)

      /**************首先由前端判断是否数据全部填完 ，否则报错。通过后再向后端发送信息*/
      let _sid = this.data.inUserInfo[0].studentID
      let _name = this.data.inUserInfo[0].name
      let _class = this.data.inUserInfo[0].class
      let _phone = this.data.inUserInfo[0].telephone
      let _email = this.data.inUserInfo[0].email
      let _main_dep = this.data.inUserInfo[1].primary
      let _mastered = this.data.inUserInfo[2].learn
      let _want_learn = this.data.inUserInfo[2].want
      let _campus = this.data.inUserInfo[3].campus
      let _tpid = this.data.t_changetpId
      if (_sid != '' && _name != '' && _class != '' && _phone != '' && _email != '' && _main_dep != ''
        && _mastered != '' && _want_learn != '' && _campus != '' && _tpid != '') {
        if (this.data.inUserInfo[0].id == '') {
          let aa = wx.getStorageSync("token")
          wx.request({
            url: 'https://zhaoxin2021.tiaozhan.com/api/student',
            data: {
              sid: this.data.inUserInfo[0].studentID,
              name: this.data.inUserInfo[0].name,
              class: this.data.inUserInfo[0].class,
              phone: this.data.inUserInfo[0].telephone,
              email: this.data.inUserInfo[0].email,
              main_dep: this.data.inUserInfo[1].primary ? this.data.inUserInfo[1].primary : undefined,
              sub_dep: this.data.requestSecondary,
              mastered: this.data.inUserInfo[2].learn ? this.data.inUserInfo[2].learn : undefined,
              want_learn: this.data.inUserInfo[2].want ? this.data.inUserInfo[2].want : undefined,
              campus: this.data.inUserInfo[3].campus,
              tpid: this.data.t_changetpId

            },
            header: {
              'cookie': aa,
              'content-type': 'application/json'
            },
            method: 'POST',
            success(res) {
              if (res.data.success == 1) {
                let aa = wx.getStorageSync("token")
                wx.request({
                  url: 'https://zhaoxin2021.tiaozhan.com/api/timeplace/changeIsChosen',
                  data: { id: that.data.t_changetpId, value: 1 },
                  header: {
                    'cookie': aa,
                    'content-type': 'application/json'
                  },
                  method: 'PUT',
                  success(res) {
                    if (res.data.success == 1) {
                      wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000
                      })
                      setTimeout(()=>{
                        wx.navigateTo({
                           url: '../index/index'
                        })  
                      },1800)
                    
                    } else {
                      wx.showModal({
                        title: '提示',
                        content: '网络异常，请重新选择面试时间或联系管理员',
                        icon: 'none',
                        duration: 2000
                      })
                    }
                  }


                })
                that.setData({ 'that.data.t_changetpId': '' })
              }
              else {
                console.log(1234)
                wx.showModal({
                  title: '提示',
                  content: '您填写的信息有误，请重新填写或联系管理员',
                  icon: 'none',
                  duration: 2000
                })
              }
            },

          })

        } else {
          // *********辛洲第四次后端设计*******

          // *********************************
          let aa = wx.getStorageSync("token")
          wx.request({
            url: 'https://zhaoxin2021.tiaozhan.com/api/student',
            data: {
              id: this.data.inUserInfo[0].id,
              sid: this.data.inUserInfo[0].studentID,
              name: this.data.inUserInfo[0].name,
              class: this.data.inUserInfo[0].class,
              phone: this.data.inUserInfo[0].telephone,
              email: this.data.inUserInfo[0].email,
              main_dep: this.data.inUserInfo[1].primary ? this.data.inUserInfo[1].primary : undefined,
              sub_dep: this.data.requestSecondary,
              mastered: this.data.inUserInfo[2].learn ? this.data.inUserInfo[2].learn : undefined,
              want_learn: this.data.inUserInfo[2].want ? this.data.inUserInfo[2].want : undefined,
              campus: this.data.inUserInfo[3].campus,
              tpid: this.data.t_changetpId ? this.data.t_changetpId : undefined

            },
            header: {
              'cookie': aa,
              'content-type': 'application/json'
            },

            method: 'PUT',

            success(res) {
              if (res.data.success == 1) {
                // ####################################################
                let aa = wx.getStorageSync("token")
                if (that.data.t_changetpId != that.data.t_basetpId) {
                  wx.request({
                    url: 'https://zhaoxin2021.tiaozhan.com/api/timeplace/changeIsChosen',
                    data: {
                      id: that.data.t_changetpId,
                      value: 1
                    },
                    header: {
                      'cookie': aa,
                      'content-type': 'application/json'
                    },

                   method: 'PUT',
                    success(res) {
                      if (res.data.success == 1) {
                        wx.showToast({
                          title: '提交成功',
                          icon: 'success',
                          duration: 2000
                        })
                      } else {
                        console.log(1234321)
                        wx.showModal({
                          title: '提示',
                          content: '网络异常，请重新选择面试时间或联系管理员',
                          icon: 'none',
                          duration: 2000
                        })
                      }
                    }
                  })


                  /****************************************************** */
                  wx.request({
                    url: 'https://zhaoxin2021.tiaozhan.com/api/timeplace/changeIsChosen',
                    data: {
                      id: that.data.t_basetpId,
                      value: -1
                    },
                    header: {
                      'cookie': aa,
                      'content-type': 'application/json'
                    },
                    method: 'PUT',
                    success(res) {
                      if (res.data.success == 1) {
                        wx.showToast({
                          title: '提交成功',
                          icon: 'success',
                          duration: 2000
                        })
                        // setTimeout(()=>{
                        //   wx.navigateTo({
                        //      url: '../index/index'
                        //   })  
                        // },3000)
                      } else {
                        console.log(1234321)
                        wx.showModal({
                          title: '提示',
                          content: '网络异常，请重新选择面试时间或联系管理员',
                          icon: 'none',
                          duration: 2000
                        })
                      }
                    }

                  })

                  that.setData({
                    't_basetpId': '',
                    't_changetpId': ''
                  })

                }
                // #######################################################
                wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 2000
                  
                }) 
                setTimeout(()=>{
                  wx.navigateTo({
                     url: '../index/index'
                  })  
                },1000)
            
                 

              } else {
                wx.showModal({
                  title: '提示',
                  content: '您填写的信息有误，请重新填写或联系管理员',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      } 
      else if(_sid == '') {
        wx.showModal({
          title: '提示',
          content: '请填写学号',
          icon: 'none',
          duration: 2000
        })
      }
      else if(_name == ''){
        wx.showModal({
          title: '提示',
          content: '请填写姓名',
          icon: 'none',
          duration: 4800
        })
      }
      else if(_class == ''){
        wx.showModal({
          title: '提示',
          content: '请填写班级',
          icon: 'none',
          duration: 2000
        })
      }
      else if(_phone == ''){
       
        wx.showModal({
          title: '提示',
          content: '请填写手机号码',
          icon: 'none',
          duration: 2000
        })
      }
      else if(_email == ''){
        wx.showModal({
          title: '提示',
          content: '请填写邮箱',
          icon: 'none',
          duration: 2000
        })
      }
      else if(_main_dep == ''){
        wx.showModal({
          title: '提示',
          content: '请填写主选部门',
          icon: 'none',
          duration: 2000
        })
      }
      else if(_mastered == ''){
        wx.showModal({
          title: '提示',
          content: '请填写已会内容',
          icon: 'none',
          duration: 2000
        })
      }
      else if(_want_learn == ''){
        wx.showModal({
          title: '提示',
          content: '请填写想学内容',
          icon: 'none',
          duration: 2000
        })
      }
      else if(_campus ==''){
        wx.showModal({
          title: '提示',
          content: '请填写校区',
          icon: 'none',
          duration: 2000
        })
      }
      else if(_tpid == ''){
        wx.showModal({
          title: '提示',
          content: '请填写面试时间地点',
          icon: 'none',
          duration: 2000
        })
      }
    },
    onLoad() {
    
    },
  }
})












































